package study.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.*;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class UaosController {
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	LoadBalancerClient LoadBalancerClient;

	String gateway =  "http://API-GATEWAY";
//	String gateway =  "http://api-gateway.jd.com:1602";
//	String gateway =  "http://192.168.186.11:1602";
	public static final String GEN_SCOPE_CODE_URL = "/jacp-uaos/uaos/api/v1/user?erp=chenhengyu1";
	String appId = "jcase";
	String appSecret = "1d64874d-8319-4b14-9182-eb5eaf11cd8b";

	/*
		http://127.0.0.1:8082/test
	 */
	@GetMapping("/uaos")
//	@HystrixCommand(fallbackMethod = "selectByIdFallback",
//		commandProperties = {@HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "true"),
//				             @HystrixProperty(name = "fallback.enabled", value = "true")})
	public String selectById() {

		JSONObject param = new JSONObject();
		HttpEntity<JSONObject> request = new HttpEntity<>(param, genHeaders());
		System.out.println("request: " + request);
		String url = String.format("%s%s", gateway, GEN_SCOPE_CODE_URL);
		System.out.println("url:" + url);
		ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
		System.out.println("responseEntity: " + response);
		String body = response.getBody();
		System.out.println("==============");
		System.out.println("body: " + body);
		JSONObject jsonObjectBody = JSONObject.parseObject(body);
		System.out.println("JSONObject: "+  jsonObjectBody);
		Integer statusCode = jsonObjectBody.getInteger("code");
		System.out.println("statusCode: " + statusCode);

		if (HttpStatus.OK.value() != statusCode) {
//            logger.warn("获取scopeCode失败！");
			System.out.println("获取scopeCode失败！");
			return null;
		}

		String s = jsonObjectBody.toJSONString();
		System.out.println("s: " + s);

		return s;
	}


	private HttpHeaders genHeaders() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		headers.add("appId", appId);
		Long timestamp = System.currentTimeMillis();
		headers.add("timestamp", String.valueOf(timestamp));
		headers.add("signature", getSignature(timestamp));
        headers.add("optErp", "chenhengyu1");
//        headers.add("optErp", userHelper.getCurrentUserErp());
		return headers;
	}

	private String getSignature(Long timestamp) {
		//签名的明文信息，由应用编码、时间戳、和密钥顺序拼接组成
		String plainText = appId + timestamp + appSecret;
		//生成的签名
		String signature = DigestUtils.md5DigestAsHex(plainText.getBytes());
		System.out.println("signature: " + signature);
		return signature;
	}

	public String selectByIdFallback() {
		return "fall back";
	}

}
